package com.feng.demo.jakartacommons.mina;

import java.nio.charset.CharsetDecoder;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class ClientHanlder extends IoHandlerAdapter {
	private static Logger logger = Logger.getLogger(ClientHanlder.class);
	public final static String stream_xml = "<?xml version='1.0' encoding='UTF-8'?>";
	public final static String stream_send = stream_xml + "<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" xmlns=\"jabber:server\" to=\"im-connector.com\" version=\"1.0\" >";
	public final static String stream_stream = "<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" xmlns=\"jabber:server\" from=\"im-connector.com\" to=\"megafon.ru\" id=\"{ID}\" xml:lang=\"en\" version=\"1.0\">";
	public final static String stream_stream_noto = "<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" xmlns=\"jabber:server\" from=\"im-connector.com\" id=\"{ID}\" xml:lang=\"en\" version=\"1.0\">";
	public final static String stream_recv = stream_xml + stream_stream;
	public final static String features_recv = "<stream:features><starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"><required/></starttls><bidi xmlns=\"urn:xmpp:bidi\"/><mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms></stream:features>";
	public final static String features_recv2 = "<stream:features><bidi xmlns=\"urn:xmpp:bidi\"/><mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms></stream:features>";
	public final static String features_recv3 = "<stream:features></stream:features>";
//	private static final String CHARSET = "UTF-8";
	CharsetDecoder decoder;
	public void sessionOpened(IoSession session) throws Exception {
		session.getConfig().getReadBufferSize();session.getConfig().setUseReadOperation(false);
		session.write(stream_send);
		//session.write("</stream:stream>");
	}

	// 当一个客户端关闭时
	public void sessionClosed(IoSession session) {
		logger.info("I'm Client &&  I closed!");
	}

	// 当服务器端发送的消息到达时:
	public void messageReceived(IoSession session, Object message) throws Exception {
		//super.messageReceived(session, message);
		logger.info("Msg RCV:"+message.toString());
		// // Write the received data back to remote peer
		// // 测试将消息回送给客户端 session.write(s);
		logger.info(session.getId());
		String expression = message.toString(); 
        if ("quit".equalsIgnoreCase(expression.trim())) { 
            session.close(true); 
            return; 
        }
        session.write("<bidi xmlns='urn:xmpp:bidi'/>");
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		// TODO Auto-generated method stub
		super.exceptionCaught(session, cause);
		logger.error("exceptionCaught:",cause);
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		super.messageSent(session, message);
		logger.debug("Msg SND:"+message.toString());
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		super.sessionCreated(session);
		logger.info("sessionCreated !");
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		// TODO Auto-generated method stub
		super.sessionIdle(session, status);
		logger.info("sessionIdle ! status:"+status);
	}
}
